function preparePhotoUpload() {
    str = '<div id="attach_header">' + 
          '<span id="header_text">Select any image file to attach</span>' + 
          '</div>' + '<div id="uploadDiv">' + 
          '<div id="uploadItems">' + 
          '<form id="uploadForm" enctype="multipart/form-data" method="post" target="uploadFrame" action="/pub/UploadImage">' + 
          '<input type="file" id="filebrowser" name="datafile" style="border: Solid 1px #D1D1D1"/>' + 
          '<span id="upload_percent" class="upload_percent">' + 
          '</span>' + 
          '</form>' + 
          '</div>' + 
          '<div id="uploadProgress" style="display: none">' + 
            '<span>Uploading</span' + 
            '<img src="/images/ajax-loading.gif" width="15" height="15" border="0" style="margin-left: 3px; vertical-align: middle;"/>' +
          '</div>'+
          '</div>';
    return str;
}

function prepareVideoUpload() {
    str = '<div id="attach_header">' + 
          '<span id="header_text">Select any video file to attach</span>' + 
          '</div>' + '<div id="uploadDiv">' + 
          '<div id="uploadItems">' + 
          '<form id="uploadForm" enctype="multipart/form-data" method="post" target="uploadFrame" action="/pub/UploadVideo">' + 
          '<input type="file" id="filebrowser" name="datafile" style="border: Solid 1px #D1D1D1"/>' + 
          '<span id="upload_percent" class="upload_percent">' + 
          '</span>' + 
          '</form>' + 
          '</div>' + 
          '<div id="uploadProgress" style="display: none">' + 
            '<span>Uploading</span' + 
            '<img src="/images/ajax-loading.gif" width="15" height="15" border="0" style="margin-left: 3px; vertical-align: middle;"/>' +
          '</div>'+
          '</div>';
    return str;
}

function addLi(jsonObj) {    
    xli = '<li id="al_' + jsonObj.f_id + '" class="' + jsonObj.f_type + '">' + 
          '<span class="info" id="info_' + jsonObj.f_id + '">' + 
            '<a href="javascript: void(0);" rel="' + jsonObj.f_url + '" class="si" id="' + jsonObj.f_id + '">' + 
                jsonObj.f_name + 
            '</a>' + 
          '</span>' + 
          '<span class="tools">' + 
            '<a href="javascript: void(0);" rel="' + jsonObj.f_id + '" class="ra">Remove</a>' + 
          '</span>' + '<hr/>' + '</li>';

    $("#attachedList").append(xli);    
}

function checkForUpload() {
    $(document).everyTime(500, "uploadReport", function (i) {
        var ih = $("#uploadFrameID").contents().find("pre").html();

        if (ih != null && ih != "" && ih.length > 0) {
            $("#filebrowser").val("");

            var jsonObj = JSON.parse(ih);
            addLi(jsonObj);
            $("#al_" + jsonObj.f_id).slideDown("fast");


            $("#attachedList > li > span.info > a.si").unbind('click');
            $("#attachedList > li > span.tools > a.ra").unbind('click');            

            $("#attachedList > li > span.tools > a.ra").click(function () {
                var id = this.rel;
                $.ajax( {
                    data : 'type=img&id=' + id, type : "POST", url : "/pub/RemoveAttachment", cache : false, success : function (data) {
                        if (data == 2) {
                            $("#al_" + id).slideUp("normal", function () {
                                $("#al_" + id).remove();
                            });
                        }
                    }
                });
            });

            $("#attachedList > li > span.info > a.si").click(function () {
                var id = this.id;
                var t = this.text;

                xi = '<span>' + this.text + '</span>' + '<img src="' + this.rel + '" width="50"/>';

                $("#" + this.id).remove();
                $("#info_" + this.id).append(xi);
            });

            $("#attachedList > li").unbind('hover');

            $("#attachedList > li").hover(function () {
                $(this).addClass("st");
            },
            function () {
                $(this).removeClass("st");
            });

            $("#uploadItems").show();
            $("#uploadProgress").hide();
            $("#uploadFrameID").contents().find("body").html("");
            $(document).stopTime("uploadReport");
        }
    });
}

function prepareFb() {
    $("#filebrowser").change(function () {
                if ($(this).val() == null || $(this).val() == "") {
                    return;
                }

                $("#uploadItems").hide();
                $("#uploadProgress").show();
                checkForUpload();
                $("#uploadForm").submit();
    });
}

function tableHover() {
    $("table.rp tr").unbind('hover');
    $("table.rp tr").hover(function () {
        $(this).addClass("tr-hover");
    },
    function () {
        $(this).removeClass("tr-hover");
    });
}

function close_dialog() {
    $("#modalClose").click();
}
$(document).ready(function () {
    $("#date_select").val(3);
    $("#date_part").css("display", "none");
    $("#time_part").css("display", "none");
    
    $("#app2").colorbox( {
        fixedWidth : 650, fixedHeight : 480, iframe : true, overlayClose : false
    });
    
    $("#app2").click(function() {
        $("#attach_content").slideUp("fast");
    });
    
    $("#app1").click(function () {
        $("#attach_content").slideUp("fast", function () {
            $("#attach_content").html(preparePhotoUpload());
            prepareFb();
            $("#attach_content").slideDown("normal");
        });
    });

    $("#app3").click(function () {
        $("#attach_content").slideUp("fast", function () {
            $("#attach_content").html(prepareVideoUpload());
            prepareFb();
            $("#attach_content").slideDown("normal");
        });
    });

    $("#submit_button").click(function () {
        var str = $("#event_form").serialize();
        $.ajax( {
            type : "POST", url : "/pub/Submit", data : str, beforeSend : function () {
                startLoadingA();
                disableMessageBox();
                disableItems();
            },
            success : function (data) {
                processRecentPosts(data);
            },
            complete : function () {
                stopLoadingA();
                enableMessageBox();
                enableItems();
                $("#attachedList").html("");
                $("#message_box").val("");
            }
        });

        return false;
    });

    $("#prevE").click(function () {
        var href = $("#prevE").attr("href");
        var lnk = href.substr(href.indexOf("start=") + 6, 10);
        var str = "start=" + (parseInt(lnk));

        $.ajax( {
            type : "POST", url : "/pub/Events", data : str, beforeSend : function () {
                startLoadingA();
                disableItems();
            },
            success : function (data) {
                processRecentPosts(data);
                var xhref = $("#prevE").attr("href");
                var xlnk = xhref.substr(xhref.indexOf("start=") + 6, 10);
                var strPrev = "start=" + (parseInt(xlnk) - 15 < 0 ? 0 : parseInt(xlnk) - 15);
                var strNext = "start=" + ((parseInt(xlnk) - 15 < 0 ? 0 : parseInt(xlnk) - 15) + 15);

                $("#prevE").attr("href", "/home.jsp#?" + strPrev);
                $("#nextE").attr("href", "/home.jsp#?" + strNext);
            },
            complete : function () {
                stopLoadingA();
                enableItems();
            }
        });
    });

    $("#nextE").click(function () {
        var href = $("#nextE").attr("href");
        var lnk = href.substr(href.indexOf("start=") + 6, 10);
        var str = "start=" + (parseInt(lnk));

        $.ajax( {
            type : "POST", url : "/pub/Events", data : str, beforeSend : function () {
                startLoadingA();
                disableItems();
            },
            success : function (data) {
                processRecentPosts(data);
            },
            complete : function () {
                stopLoadingA();
                enableItems();

                var xhref = $("#nextE").attr("href");
                var xlnk = xhref.substr(xhref.indexOf("start=") + 6, 10);
                var strPrev = "start=" + (parseInt(xlnk) - 15 < 0 ? 0 : parseInt(xlnk) - 15);
                var strNext = "start=" + (parseInt(xlnk) + 15);

                $("#prevE").attr("href", "/home.jsp#?" + strPrev);
                $("#nextE").attr("href", "/home.jsp#?" + strNext);
            }
        });

        $("#nextE").attr("href", "/home.jsp#?" + strNext);
        $("#prevE").attr("href", "/home.jsp#?" + strPrev);
    });

    $("#date_select").change(function () {
        var ds = $(this).val();

        if (ds <= 2) {
            $("#time_part").customFadeIn("normal");
            $("#date_part").customFadeOut("normal");
        }
        else if (ds == 3) {
            $("#time_part").customFadeOut("normal");
            $("#date_part").customFadeOut("normal");
        }
        else if (ds == 4 || ds == 5) {
            $("#time_part").customFadeIn("normal");
            $("#date_part").customFadeOut("normal");

        }
        else if (ds == 6) {
            $("#date_part").customFadeIn("normal");
            $("#time_part").customFadeIn("normal");
        }
    });

    tableHover();
    $(".toolbox ul li").hover(function () {
        $(this).addClass("tools-hover");
    },
    function () {
        $(this).removeClass("tools-hover");
    });

    $(".toolbox2 ul li").hover(function () {
        $(this).addClass("tools-hover");
    },
    function () {
        $(this).removeClass("tools-hover");
    });

});

function stopLoadingA() {
    $("#ajax_load").html("");
}

function enableItems() {
    $("#recentposts").fadeTo("fast", 1);
}

function enableMessageBox() {
    $("#date_select").removeAttr("disabled");
    $("#date_month").removeAttr("disabled");
    $("#date_day").removeAttr("disabled");
    $("#time_hour").removeAttr("disabled");
    $("#time_minute").removeAttr("disabled");
    $("#message_box").removeAttr("disabled");
    $("#submit_button").removeAttr("disabled");
}

function startLoadingA() {
    var str = '<img src="/images/ajax-loading.gif" width="15" height="15" border="0" style="vertical-align: middle;"/>';
    $("#ajax_load").html(str);
}

function disableMessageBox() {
    $("#date_select").attr("disabled", "disabled");
    $("#date_month").attr("disabled", "disabled");
    $("#date_day").attr("disabled", "disabled");
    $("#time_hour").attr("disabled", "disabled");
    $("#time_minute").attr("disabled", "disabled");
    $("#message_box").attr("disabled", "disabled");
    $("#submit_button").attr("disabled", "disabled");
}

function disableItems() {
    $("#recentposts").fadeTo("fast", 0.3);

}
(function ($) {
    $.fn.customFadeIn = function (speed, callback) {
        $(this).fadeIn(speed, function () {
            if (jQuery.browser.msie)
                $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined)
                callback();
        });
    };
    $.fn.customFadeOut = function (speed, callback) {
        $(this).fadeOut(speed, function () {
            if (jQuery.browser.msie)
                $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined)
                callback();
        });
    };
})(jQuery);

function processRecentPosts(data) {
    var eventData = JSON.parse(data);

    var table = '<table width="100%" cellpadding="5" cellspacing="0" border="0" class="rp">';

    var i = 0;
    for (i in eventData.events) {
        table += '<tr>' + '<td width="60px" class="photo">' + '<img src="' + eventData.events[i].owner.owner_photosmall + '" width="50"/>' + '</td>' + '<td>' + '<span class="event_info">' + eventData.events[i].owner.owner_fullname + '<span class="event_time">' + eventData.events[i].event_date + '</span>' + '</span>' + '<div class="event_message">' + '<span>' + eventData.events[i].event_message + '</span>' + '</div>';
        if (eventData.events[i].event_photocount > 0) {
            table += '<div style="margin-top: 10px;">' + '<a id="app1" class="applink first" href="javascript: void(0);"' + 'style="background-image: url(/images/photo-icon.png); background-position: left; background-repeat: no-repeat;">' + eventData.events[i].event_photocount + ' Images' + '</a>' + '</div>';
        }
        table += '</td>' + '</tr>';
    }
    table += '</table>';
    $("#recentposts").html(table);
    tableHover();
}
